/**
 * @文件名称: TimerAction.java
 * @类路径:   com.dssrz.weibo4j.action
 * @描述:     TODO
 * @作者:     robin
 * @时间:     2012-3-12 下午03:32:34
 * @版本:     1.0.0
 */
package com.dssrz.weibo4j.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import org.apache.log4j.Logger;

import com.dssrz.weibo4j.Timeline;
import com.dssrz.weibo4j.Weibo;
import com.dssrz.weibo4j.model.Status;
import com.dssrz.weibo4j.model.WeiboException;
import com.dssrz.weibo4j.task.PublishTask;
import com.dssrz.weibo4j.task.TaskCache;
import com.dssrz.weibo4j.task.TaskQueue;
import com.dssrz.weibo4j.timer.PublishTimerTask;
import com.dssrz.weibo4j.timer.RefreshTimerTask;

/**
 * @类功能说明:
 * @类修改者:
 * @修改日期:
 * @修改说明:
 * @作者: robin
 * @创建时间: 2012-3-12 下午03:32:34
 * @版本: 1.0.0
 */

public class TimerAction extends BaseAction {

	/**
	 * @Fields serialVersionUID: TODO
	 */
	private static final long serialVersionUID = 6466532412236814583L;

	private static final Logger log = Logger.getLogger(TimerAction.class);
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	private static Timer timer = new Timer();
	private static long taskId = 1;

	private String status;
	private String publishDate;
	private String publishHour;
	private String publishMinute;
	private int nextTimer;
	
	static {
		RefreshTimerTask rftask = new RefreshTimerTask();
		timer.schedule(rftask, 1000, 30000);	// 每30秒刷新一次定时任务信息
	}

	/**
	 * @return status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            status
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return publishDate
	 */
	public String getPublishDate() {
		return publishDate;
	}

	/**
	 * @param publishDate
	 *            publishDate
	 */
	public void setPublishDate(String publishDate) {
		this.publishDate = publishDate;
	}

	/**
	 * @return publishHour
	 */
	public String getPublishHour() {
		return publishHour;
	}

	/**
	 * @param publishHour
	 *            publishHour
	 */
	public void setPublishHour(String publishHour) {
		this.publishHour = publishHour;
	}

	/**
	 * @return publishMinute
	 */
	public String getPublishMinute() {
		return publishMinute;
	}

	/**
	 * @param publishMinute
	 *            publishMinute
	 */
	public void setPublishMinute(String publishMinute) {
		this.publishMinute = publishMinute;
	}

	/**
	 * @return nextTimer
	 */
	public int getNextTimer() {
		return nextTimer;
	}

	/**
	 * @param nextTimer
	 *            nextTimer
	 */
	public void setNextTimer(int nextTimer) {
		this.nextTimer = nextTimer;
	}

	public String timerSend() {
		String theDate = publishDate + " " + publishHour + ":" + publishMinute;
		Date excuteTime = null;
		try {
			excuteTime = DATE_FORMAT.parse(theDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		log.info("定时(" + theDate + ")发送...");
		Date now = new Date();
		long delay = excuteTime.getTime() - now.getTime();
		// 添加定时任务
		String access_token = (String) request.getSession().getAttribute(
				"access_token");
		PublishTask publishtask = new PublishTask(theDate, access_token, status);
		publishtask.setBeginExceuteTime(excuteTime);
		publishtask.setTaskId(taskId);
		taskId = (taskId % 100000000L) + 1;
		PublishTimerTask ptt = new PublishTimerTask(publishtask);
		if (delay < 0) {
			timer.schedule(ptt, 0);
		} else {
			timer.schedule(ptt, delay);
			TaskCache.addOneTask(access_token, publishtask);
		}	
		// TaskQueue.add(publishtask);

		Calendar calendar = Calendar.getInstance();
		Date now1 = new Date();
		calendar.setTime(now1);
		request.setAttribute("nowdate", now1);
		request.setAttribute("nowhour", calendar.get(Calendar.HOUR_OF_DAY));
		request.setAttribute("nowminute", calendar.get(Calendar.MINUTE));
		request.setAttribute("nextTimer", nextTimer);
		request.setAttribute("tasks", TaskCache.getTasks(access_token));
		return "timer_success";
	}

	public String nowSend() {
		log.info("立即发送...");
		String access_token = (String) request.getSession().getAttribute(
				"access_token");
		Weibo weibo = new Weibo();
		weibo.setToken(access_token);
		Timeline tm = new Timeline();
		try {
			Status sta = tm.UpdateStatus(status);
			log.info(sta.toString());
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		request.setAttribute("nowdate", date);
		request.setAttribute("nowhour", calendar.get(Calendar.HOUR_OF_DAY));
		request.setAttribute("nowminute", calendar.get(Calendar.MINUTE));
		request.setAttribute("nextTimer", nextTimer);
		log.info("发送成功!");
		return "now_success";
	}
}
